package com.eostoken.eos_wallet.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.constraintlayout.widget.Group;

import com.bumptech.glide.Glide;
import com.eostoken.eos_wallet.R;
import com.qmuiteam.qmui.layout.QMUILinearLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AppsItemView extends QMUILinearLayout {

    @BindView(R.id.smallIconIv)
    public AppCompatImageView smallIconIv;
    @BindView(R.id.titleTv)
    public AppCompatTextView titleTv;
    @BindView(R.id.detailsTitleTv)
    public AppCompatTextView detailsTitleTv;
    @BindView(R.id.bigIconIv)
    public AppCompatImageView bigIconIv;
    @BindView(R.id.nameTv)
    public AppCompatTextView nameTv;
    @BindView(R.id.detailsNameTv)
    public AppCompatTextView detailsNameTv;
    @BindView(R.id.group)
    public Group group;
    @BindView(R.id.accessoryType)
    public AppCompatImageView accessoryType;
    @BindView(R.id.appsItemLayout)
    public QMUILinearLayout appsItemLayout;

    private Context context;
    private TypedArray typedArray;

    public AppsItemView(Context context) {
        super(context);
        this.context = context;
    }

    public AppsItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        typedArray = context.obtainStyledAttributes(attrs, R.styleable.AppsItemView);
        View view = LayoutInflater.from(context).inflate(R.layout.apps_item, this);
        ButterKnife.bind(this, view);

        initData();
        typedArray.recycle();
    }

    public AppsItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        typedArray = context.obtainStyledAttributes(attrs, R.styleable.AppsItemView);
        View view = LayoutInflater.from(context).inflate(R.layout.apps_item, this);
        ButterKnife.bind(this, view);

        initData();
        typedArray.recycle();
    }


    private void initData() {
        int radius = typedArray.getDimensionPixelSize(R.styleable.AppsItemView_radius, 0);
        appsItemLayout.setRadius(radius);
        int smallIcon = typedArray.getResourceId(R.styleable.AppsItemView_small_icon, -1);
        if (smallIcon == -1) {
            smallIconIv.setVisibility(GONE);
        } else {
            smallIconIv.setVisibility(VISIBLE);
            smallIconIv.setImageResource(smallIcon);
//            Glide.with(getContext()).load(smallIcon).error(R.drawable.placeholder)
//                    .placeholder(R.drawable.placeholder).into(smallIconIv);
        }

        String title = typedArray.getString(R.styleable.AppsItemView_title);
        titleTv.setText(title);

        String detailsTitle = typedArray.getString(R.styleable.AppsItemView_details_title);
        detailsTitleTv.setText(detailsTitle);

        int bigIcon = typedArray.getResourceId(R.styleable.AppsItemView_big_icon, -1);
        if (bigIcon == -1) {
            bigIconIv.setVisibility(GONE);
        } else {
            bigIconIv.setVisibility(VISIBLE);
            bigIconIv.setImageResource(bigIcon);
//            Glide.with(getContext()).load(bigIcon).error(R.drawable.placeholder)
//                    .placeholder(R.drawable.placeholder).into(bigIconIv);
        }

        String name = typedArray.getString(R.styleable.AppsItemView_name);
        nameTv.setText(name);

        String detailsName = typedArray.getString(R.styleable.AppsItemView_details_name);
        detailsNameTv.setText(detailsName);

        boolean rightArrow = typedArray.getBoolean(R.styleable.AppsItemView_right_arrow, true);
        if (rightArrow) {
            accessoryType.setVisibility(VISIBLE);
        } else {
            accessoryType.setVisibility(GONE);
        }

    }
}
